package com.xxx.chat;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {

	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {

		//1. 创建Socket类，连接服务端
		Socket socket = null;

		OutputStreamWriter osw =null;

		InputStreamReader isr = null;

		OutputStream outputStream = null;

		InputStream inputStream = null;

		System.out.println("------------------------客户端---------------------------");

				{
			try {

				Boolean flag = true;
				while (flag) {

					socket = new Socket("192.168.2.130" , 1025);

					//2. 访问服务端
					outputStream = socket.getOutputStream();

					//3. 将信息发送至服务端
					osw = new OutputStreamWriter(outputStream,"GBK");
//					bos = new BufferedOutputStream(outputStream);
					String msg = sc.next();

					osw.write(msg);
					if (msg.equals("拜拜")) {
						flag = false;
					}
					osw.flush();

					//4. 接受服务端的信息
					inputStream = socket.getInputStream();

					isr = new InputStreamReader(inputStream,"GBK");
					int len;
					char[] b = new char[1024];
					len = isr.read(b);
					System.out.println(new String(b,0,len));

				}

			} catch (IOException e) {
				e.printStackTrace();
			}finally {

				try {

					if (osw != null) {
						osw.close();
					}

					if (isr != null) {
						isr.close();
					}

					if (inputStream != null) {
						inputStream.close();
					}

					if (outputStream != null) {
						outputStream.close();
					}

					if (socket != null) {
						socket.close();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}


	}


}
